package com.yonyou.pmclouds.projectstatus.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.projectstatus.entity.ProjectStatusVO;
import com.yonyou.pmclouds.projectstatus.mapper.ProjectStatusMapper;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusMaintain;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusQuery;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectStatusMaintain.class)
public class ProjectStatusMaintainImpl implements ProjectStatusMaintain {
    /**
     * 更新的字段
     */
    private static final String[] UPDATE_FIELDS = {ProjectStatusVO.SNAME,
            ProjectStatusVO.SCODE, ProjectStatusVO.ENABLE_STATUS, ProjectStatusVO.STATE_SORT,
            CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};

    @Autowired
    private ProjectStatusMapper mapper;
    @Autowired
    private ProjectStatusQuery projectStatusQuery;

    @Override
    public ProjectStatusVO insert(ProjectStatusVO projectStatusVO) throws BusinessException {
        SingleInsertBpTemplate<ProjectStatusVO> bp = new SingleInsertBpTemplate<>(mapper);
        projectStatusVO.setPkTenant(RuntimeEnvironment.getTenantId());
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ProjectStatusVO>());

        return bp.insert(projectStatusVO);
    }

    @Override
    public String delete(String pkProjectStatus, String changeFlag) throws BusinessException {
        ProjectStatusVO projectStatusVO = new ProjectStatusVO();
        projectStatusVO.setPkProjectStatus(pkProjectStatus);
        projectStatusVO.setChangeFlag(changeFlag);
        SingleDeleteBpTemplate<ProjectStatusVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ProjectStatusVO>());
        bp.delete(projectStatusVO);

        return pkProjectStatus;
    }

    @Override
    public ProjectStatusVO update(ProjectStatusVO projectStatusVO) throws BusinessException {
        projectStatusVO.setPkTenant(RuntimeEnvironment.getTenantId());
        SingleUpdateBpTemplate<ProjectStatusVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ProjectStatusVO>());

        return bp.update(projectStatusVO);
    }

}
